
package com.cari.baju.yog;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cari.baju.yog.adapter.AdapterCari;
import com.cari.baju.yog.database.DatabaseHelper;
import com.cari.baju.yog.entity.E_Toko;
import com.cari.baju.yog.utils.Utils;

public class SearchResultActivity extends Activity implements OnItemClickListener, OnClickListener {

    private Bundle data;
    private Location loc;
    private ListView lvSearchResult;
    private TextView title;
    private String keyword;
    private int categoryID;
    private List<E_Toko> listToko;
    private AdapterCari adapter;
    private Button btnMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);

        data = getIntent().getExtras();

        lvSearchResult = (ListView) findViewById(R.id.lv_search_result);
        title = (TextView) findViewById(R.id.txt_title_search);
        btnMap = (Button) findViewById(R.id.btn_maps);

        lvSearchResult.setEmptyView(findViewById(android.R.id.empty));

        lvSearchResult.setOnItemClickListener(this);
        btnMap.setOnClickListener(this);

        loc = new Location("a");

        if (data != null) {
            categoryID = data.getInt("categoryID");
            title.setText("Pencarian " + data.getString("category"));
            keyword = data.getString("keyword");
            Utils.TRACE("SearchResultActivity", "onCreate : " + keyword);

            loc.setLatitude(data.getDouble("lat"));
            loc.setLongitude(data.getDouble("lng"));
        }

        // pencarian ke database
        listToko = DatabaseHelper.getInstance(this).getToko(categoryID, keyword, loc);

        if (listToko.size() > 0) {
            // jika data yang dicari ada hasilnya

            // set data ke adapter
            adapter = new AdapterCari(this, listToko);
            lvSearchResult.setAdapter(adapter);
        } else {
            Toast.makeText(this, "Tidak ada data ditemukan", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

        // pindah ke activity detail toko
        startActivity(new Intent(this, TokoActivity.class).putExtra("id",
                ((E_Toko) adapter.getItem(position)).getId()));
    }

    @Override
    public void onClick(View v) {

        // jika button map di klik
        if (v == btnMap) {
            if (listToko.size() > 0) {
                Bundle data = new Bundle();

                double lat[] = new double[listToko.size()];
                double lng[] = new double[listToko.size()];
                int idLoc[] = new int[listToko.size()];
                String title[] = new String[listToko.size()];
                for (int i = 0; i < listToko.size(); i++) {
                    lat[i] = listToko.get(i).getLat();
                    lng[i] = listToko.get(i).getLng();
                    idLoc[i] = listToko.get(i).getId();
                    title[i] = listToko.get(i).getName();
                }

                data.putDoubleArray("lat", lat);
                data.putDoubleArray("lng", lng);
                data.putIntArray("id", idLoc);
                data.putStringArray("title", title);

                // tampilkan ke map
                startActivity(new Intent(this, MapActivity.class).putExtras(data));
            }
        }
    }
}
